package com.example.xiaodu.yuejian12.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.xiaodu.yuejian12.Bean.LoginBean;
import com.example.xiaodu.yuejian12.Listener.RetrofitListener;
import com.example.xiaodu.yuejian12.MainActivity;
import com.example.xiaodu.yuejian12.Model.UserModel;
import com.example.xiaodu.yuejian12.R;

public class Login extends AppCompatActivity {
    private EditText username,userpass;
    private Button login,register;
    private Intent intent,intent2;
    private UserModel userModel;
    private RetrofitListener<LoginBean> listener;



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        username = (EditText) findViewById(R.id.edit_username);
        userpass = (EditText) findViewById(R.id.edit_password);
        login = (Button) findViewById(R.id.btn_login);
        register = (Button) findViewById(R.id.btn_register);

        listener=new RetrofitListener<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean) {
                if ("1".equals(loginBean.getFlag())) {
                    Toast.makeText(Login.this, "登陆成功", Toast.LENGTH_LONG).show();
                    intent = new Intent(Login.this, MainActivity.class);
                    startActivity(intent);
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(Login.this, msg, Toast.LENGTH_LONG).show();

            }
        };


        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               userModel=new UserModel();
                userModel.userLogin(username.getText().toString(),userpass.getText().toString(),listener);
            }
        });

        register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                intent2 = new Intent(Login.this,Register.class);
                startActivity(intent2);
            }
        });
    }



    }

